﻿using Domain.Model;
using Domain.Model.Enums;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    /// <summary>
    /// 财务管理
    /// </summary>
    public class FinanceController : LoginedController
    {
        //
        // GET: /Finance/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult IndexPage(int pageIndex, int pageSize)
        {
            var q = from l in this.DbContext.Withdraw
                    select l;
            var page = q.OrderByDescending(item => item.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }

        #region 汇款充值
        public ActionResult Pay()
        {
            return View();
        }
        public ActionResult PayPage(int pageIndex, int pageSize)
        {
            var q = from l in this.DbContext.Pay
                    select l;
            var page = q.OrderByDescending(item => item.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        public ActionResult ChagnePayState(Guid id,State state)
        {
            try
            {
                var pay = DbContext.Pay.Find(id);
                pay.State = state;
                DbContext.SaveChanges();
                return Json(new { state = false, value ="success"});
            }
            catch (Exception ex)
            {
                return Json(new { state=false,value=ex.Message});
            }
        }
        #endregion

        public ActionResult Token()
        {
            return View();
        }
        public ActionResult TokenPage(int pageIndex, int pageSize)
        {
            var q = from l in this.DbContext.Pay
                    where l.UserID == this.LoginUser.ID
                    select l;
            var page = q.OrderByDescending(item => item.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }

        #region 转账管理
        public ActionResult Transfer()
        {
            return View();
        }
        public ActionResult TransferPage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<Transfer>();
            if (keyword != null)
            {
                where = where.And(i => i.MenberOut.Contains(keyword) || i.UserIn.Contains(keyword));
            }
            var page = DbContext.Transfer.Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion

        #region 会员财务汇总
        public ActionResult FinanceCollect()
        {
            return View();
        }
        public ActionResult FinanceCollectPage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<FinanceCollect>();
            if (keyword != null)
            {
                where = where.And(i => i.NickName.Contains(keyword));
            }
            var page = DbContext.FinanceCollect
                .Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion

        #region 财务明细
        public ActionResult FinanceDetail()
        {
            return View();
        }
        public ActionResult FinanceDetailPage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<FinanceDetail>();
            if (keyword != null)
            {
                where = where.And(i => i.MemberNo.Contains(keyword));
            }
            var page = DbContext.FinanceDetail
                .Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion

        #region 奖金明细
        public ActionResult FinanceBonusDetail()
        {
            return View();
        }
        public ActionResult FinanceBonusDetailPage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<FinanceBonusDetail>();
            if (keyword != null)
            {
                where = where.And(i => i.MemberNo.Contains(keyword)||i.BonusName.Contains(keyword));
            }
            var page = DbContext.FinanceBonusDetail
                .Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion

        #region 奖金报表
        public ActionResult FinanceBonusReport()
        {
            return View();
        }
        public ActionResult FinanceBonusReportPage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<FinanceBonusReport>();
            if (keyword != null)
            {
                where = where.And(i => i.MemberNo.Contains(keyword) || i.NickName.Contains(keyword));
            }
            var page = DbContext.FinanceBonusReport
                .Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion

        #region 拨出率统计
        public ActionResult FinanceAsideRate()
        {
            return View();
        }
        public ActionResult FinanceAsideRatePage(int pageIndex, int pageSize, string keyword)
        {
            var where = Where.True<FinanceAsideRate>();
            var page = DbContext.FinanceAsideRate
                .Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        #endregion
    }
}
